home *** CD-ROM | disk | FTP | other *** search
/ Cre@te Online 2000 December / Cre@teOnline CD05.iso / MacSoft / XML ConsoleMax.sea / XML ConsoleMax / Required / parser.jar / com / sun / xml / parser / InputEntity.class (.txt) < prev    next >
Encoding:
Java Class File  |  2000-02-23  |  10.5 KB  |  710 lines

  1. package com.sun.xml.parser;
  2.  
  3. import com.sun.xml.util.XmlChars;
  4. import java.io.CharConversionException;
  5. import java.io.IOException;
  6. import java.io.InputStream;
  7. import java.io.InputStreamReader;
  8. import java.io.Reader;
  9. import java.io.UnsupportedEncodingException;
  10. import java.net.URL;
  11. import java.util.Locale;
  12. import org.xml.sax.DocumentHandler;
  13. import org.xml.sax.ErrorHandler;
  14. import org.xml.sax.InputSource;
  15. import org.xml.sax.Locator;
  16. import org.xml.sax.SAXException;
  17. import org.xml.sax.SAXParseException;
  18.  
  19. final class InputEntity implements Locator {
  20.    private int start;
  21.    private int finish;
  22.    private char[] buf;
  23.    private int lineNumber = 1;
  24.    private boolean returnedFirstHalf = false;
  25.    private boolean maybeInCRLF = false;
  26.    private String name;
  27.    private InputEntity next;
  28.    private InputSource input;
  29.    private Reader reader;
  30.    private boolean isClosed;
  31.    private ErrorHandler errHandler;
  32.    private Locale locale;
  33.    private StringBuffer rememberedText;
  34.    private int startRemember;
  35.    private boolean isPE;
  36.    private static final int BUFSIZ = 8193;
  37.    private static final char[] newline = new char[]{'\n'};
  38.  
  39.    private InputEntity() {
  40.    }
  41.  
  42.    private void checkRecursion(InputEntity var1) throws SAXException {
  43.       if (var1 != null) {
  44.          for(InputEntity var2 = var1.next; var2 != null; var2 = var2.next) {
  45.             if (var2.name != null && var2.name.equals(this.name)) {
  46.                this.fatal("P-069", new Object[]{this.name});
  47.             }
  48.          }
  49.  
  50.       }
  51.    }
  52.  
  53.    private boolean checkSurrogatePair(int var1) throws SAXException {
  54.       if (var1 + 1 >= this.finish) {
  55.          return false;
  56.       } else {
  57.          char var2 = this.buf[var1++];
  58.          char var3 = this.buf[var1];
  59.          if (var2 >= '\ud800' && var2 < '\udc00' && var3 >= '\udc00' && var3 <= '\udfff') {
  60.             return true;
  61.          } else {
  62.             this.fatal("P-074", new Object[]{Integer.toHexString(var2 & '\uffff'), Integer.toHexString(var3 & '\uffff')});
  63.             return false;
  64.          }
  65.       }
  66.    }
  67.  
  68.    public void close() {
  69.       try {
  70.          if (this.reader != null && !this.isClosed) {
  71.             this.reader.close();
  72.          }
  73.  
  74.          this.isClosed = true;
  75.       } catch (IOException var1) {
  76.       }
  77.  
  78.    }
  79.  
  80.    private void fatal(String var1, Object[] var2) throws SAXException {
  81.       SAXParseException var3 = new SAXParseException(Parser.messages.getMessage(this.locale, var1, var2), this);
  82.       this.close();
  83.       this.errHandler.fatalError(var3);
  84.       throw var3;
  85.    }
  86.  
  87.    private void fillbuf() throws IOException, SAXException {
  88.       if (this.reader != null && !this.isClosed) {
  89.          if (this.startRemember != 0) {
  90.             if (this.rememberedText == null) {
  91.                this.rememberedText = new StringBuffer(this.buf.length);
  92.             }
  93.  
  94.             this.rememberedText.append(this.buf, this.startRemember, this.start - this.startRemember);
  95.          }
  96.  
  97.          boolean var1 = this.finish > 0 && this.start > 0;
  98.          if (var1) {
  99.             --this.start;
  100.          }
  101.  
  102.          int var2 = this.finish - this.start;
  103.          System.arraycopy(this.buf, this.start, this.buf, 0, var2);
  104.          this.start = 0;
  105.          this.finish = var2;
  106.  
  107.          try {
  108.             var2 = this.buf.length - var2;
  109.             var2 = this.reader.read(this.buf, this.finish, var2);
  110.          } catch (UnsupportedEncodingException var4) {
  111.             this.fatal("P-075", new Object[]{((Throwable)var4).getMessage()});
  112.          } catch (CharConversionException var5) {
  113.             this.fatal("P-076", new Object[]{((Throwable)var5).getMessage()});
  114.          }
  115.  
  116.          if (var2 >= 0) {
  117.             this.finish += var2;
  118.          } else {
  119.             this.close();
  120.          }
  121.  
  122.          if (var1) {
  123.             ++this.start;
  124.          }
  125.  
  126.          if (this.startRemember != 0) {
  127.             this.startRemember = 1;
  128.          }
  129.  
  130.       }
  131.    }
  132.  
  133.    public int getColumnNumber() {
  134.       return -1;
  135.    }
  136.  
  137.    public String getEncoding() {
  138.       if (this.reader == null) {
  139.          return null;
  140.       } else if (this.reader instanceof XmlReader) {
  141.          return ((XmlReader)this.reader).getEncoding();
  142.       } else {
  143.          return this.reader instanceof InputStreamReader ? ((InputStreamReader)this.reader).getEncoding() : null;
  144.       }
  145.    }
  146.  
  147.    public static InputEntity getInputEntity(ErrorHandler var0, Locale var1) {
  148.       InputEntity var2 = new InputEntity();
  149.       var2.errHandler = var0;
  150.       var2.locale = var1;
  151.       return var2;
  152.    }
  153.  
  154.    public int getLineNumber() {
  155.       Locator var1 = this.getLocator();
  156.       return var1 == this ? this.lineNumber : var1.getLineNumber();
  157.    }
  158.  
  159.    private Locator getLocator() {
  160.       InputEntity var1;
  161.       for(var1 = this; var1 != null && var1.input == null; var1 = var1.next) {
  162.       }
  163.  
  164.       return var1 == null ? this : var1;
  165.    }
  166.  
  167.    public String getName() {
  168.       return this.name;
  169.    }
  170.  
  171.    public char getNameChar() throws IOException, SAXException {
  172.       if (this.finish <= this.start) {
  173.          this.fillbuf();
  174.       }
  175.  
  176.       if (this.finish > this.start) {
  177.          char var1 = this.buf[this.start++];
  178.          if (XmlChars.isNameChar(var1)) {
  179.             return var1;
  180.          }
  181.  
  182.          --this.start;
  183.       }
  184.  
  185.       return '\u0000';
  186.    }
  187.  
  188.    public String getPublicId() {
  189.       Locator var1 = this.getLocator();
  190.       return var1 == this ? this.input.getPublicId() : var1.getPublicId();
  191.    }
  192.  
  193.    public String getSystemId() {
  194.       Locator var1 = this.getLocator();
  195.       return var1 == this ? this.input.getSystemId() : var1.getSystemId();
  196.    }
  197.  
  198.    public char getc() throws IOException, SAXException {
  199.       if (this.finish <= this.start) {
  200.          this.fillbuf();
  201.       }
  202.  
  203.       if (this.finish > this.start) {
  204.          char var1 = this.buf[this.start++];
  205.          if (this.returnedFirstHalf) {
  206.             if (var1 >= '\udc00' && var1 <= '\udfff') {
  207.                this.returnedFirstHalf = false;
  208.                return var1;
  209.             }
  210.  
  211.             this.fatal("P-070", new Object[]{Integer.toHexString(var1)});
  212.          }
  213.  
  214.          if (var1 >= ' ' && var1 <= '\ud7ff' || var1 == '\t' || var1 >= '\ue000' && var1 <= 'ÔøΩ') {
  215.             return var1;
  216.          }
  217.  
  218.          if (var1 == '\r' && !this.isInternal()) {
  219.             this.maybeInCRLF = true;
  220.             var1 = this.getc();
  221.             if (var1 != '\n') {
  222.                this.ungetc();
  223.             }
  224.  
  225.             this.maybeInCRLF = false;
  226.             ++this.lineNumber;
  227.             return '\n';
  228.          }
  229.  
  230.          if (var1 == '\n' || var1 == '\r') {
  231.             if (!this.isInternal() && !this.maybeInCRLF) {
  232.                ++this.lineNumber;
  233.             }
  234.  
  235.             return var1;
  236.          }
  237.  
  238.          if (var1 >= '\ud800' && var1 < '\udc00') {
  239.             this.returnedFirstHalf = true;
  240.             return var1;
  241.          }
  242.  
  243.          this.fatal("P-071", new Object[]{Integer.toHexString(var1)});
  244.       }
  245.  
  246.       throw new EndOfInputException();
  247.    }
  248.  
  249.    public boolean ignorableWhitespace(DocumentHandler var1) throws IOException, SAXException {
  250.       boolean var3 = false;
  251.       int var4 = this.start;
  252.  
  253.       while(true) {
  254.          if (this.finish <= this.start) {
  255.             if (var3) {
  256.                var1.ignorableWhitespace(this.buf, var4, this.start - var4);
  257.             }
  258.  
  259.             this.fillbuf();
  260.             var4 = this.start;
  261.          }
  262.  
  263.          if (this.finish <= this.start) {
  264.             return var3;
  265.          }
  266.  
  267.          char var2 = this.buf[this.start++];
  268.          switch (var2) {
  269.             case '\n':
  270.                if (!this.isInternal()) {
  271.                   ++this.lineNumber;
  272.                }
  273.             case '\t':
  274.             case ' ':
  275.                var3 = true;
  276.                break;
  277.             case '\r':
  278.                var3 = true;
  279.                if (!this.isInternal()) {
  280.                   ++this.lineNumber;
  281.                }
  282.  
  283.                var1.ignorableWhitespace(this.buf, var4, this.start - 1 - var4);
  284.                var1.ignorableWhitespace(newline, 0, 1);
  285.                if (this.start < this.finish && this.buf[this.start] == '\n') {
  286.                   ++this.start;
  287.                }
  288.  
  289.                var4 = this.start;
  290.                break;
  291.             default:
  292.                this.ungetc();
  293.                if (var3) {
  294.                   var1.ignorableWhitespace(this.buf, var4, this.start - var4);
  295.                }
  296.  
  297.                return var3;
  298.          }
  299.       }
  300.    }
  301.  
  302.    public void init(InputSource var1, String var2, InputEntity var3, boolean var4) throws IOException, SAXException {
  303.       this.input = var1;
  304.       this.isPE = var4;
  305.       this.reader = var1.getCharacterStream();
  306.       if (this.reader == null) {
  307.          InputStream var5 = var1.getByteStream();
  308.          if (var5 == null) {
  309.             this.reader = XmlReader.createReader((new URL(var1.getSystemId())).openStream());
  310.          } else if (var1.getEncoding() != null) {
  311.             this.reader = XmlReader.createReader(var1.getByteStream(), var1.getEncoding());
  312.          } else {
  313.             this.reader = XmlReader.createReader(var1.getByteStream());
  314.          }
  315.       }
  316.  
  317.       this.next = var3;
  318.       this.buf = new char[8193];
  319.       this.name = var2;
  320.       this.checkRecursion(var3);
  321.    }
  322.  
  323.    public void init(char[] var1, String var2, InputEntity var3, boolean var4) throws SAXException {
  324.       this.next = var3;
  325.       this.buf = var1;
  326.       this.finish = var1.length;
  327.       this.name = var2;
  328.       this.isPE = var4;
  329.       this.checkRecursion(var3);
  330.    }
  331.  
  332.    public boolean isDocument() {
  333.       return this.next == null;
  334.    }
  335.  
  336.    public boolean isEOF() throws IOException, SAXException {
  337.       if (this.start >= this.finish) {
  338.          this.fillbuf();
  339.          return this.start >= this.finish;
  340.       } else {
  341.          return false;
  342.       }
  343.    }
  344.  
  345.    public boolean isInternal() {
  346.       return this.reader == null;
  347.    }
  348.  
  349.    public boolean isParameterEntity() {
  350.       return this.isPE;
  351.    }
  352.  
  353.    public boolean maybeWhitespace() throws IOException, SAXException {
  354.       boolean var2 = false;
  355.       boolean var3 = false;
  356.  
  357.       while(true) {
  358.          if (this.finish <= this.start) {
  359.             this.fillbuf();
  360.          }
  361.  
  362.          if (this.finish <= this.start) {
  363.             return var2;
  364.          }
  365.  
  366.          char var1 = this.buf[this.start++];
  367.          if (var1 != ' ' && var1 != '\t' && var1 != '\n' && var1 != '\r') {
  368.             --this.start;
  369.             return var2;
  370.          }
  371.  
  372.          var2 = true;
  373.          if ((var1 == '\n' || var1 == '\r') && !this.isInternal()) {
  374.             if (var1 != '\n' || !var3) {
  375.                ++this.lineNumber;
  376.                var3 = false;
  377.             }
  378.  
  379.             if (var1 == '\r') {
  380.                var3 = true;
  381.             }
  382.          }
  383.       }
  384.    }
  385.  
  386.    public boolean parsedContent(DocumentHandler var1, ElementValidator var2) throws IOException, SAXException {
  387.       int var4;
  388.       int var3 = var4 = this.start;
  389.       boolean var5 = false;
  390.  
  391.       while(true) {
  392.          if (var4 >= this.finish) {
  393.             if (var4 > var3) {
  394.                var2.text();
  395.                var1.characters(this.buf, var3, var4 - var3);
  396.                var5 = true;
  397.                this.start = var4;
  398.             }
  399.  
  400.             if (this.isEOF()) {
  401.                return var5;
  402.             }
  403.  
  404.             var3 = this.start;
  405.             var4 = var3 - 1;
  406.          } else {
  407.             char var6 = this.buf[var4];
  408.             if ((var6 <= ']' || var6 > '\ud7ff') && (var6 >= '&' || var6 < ' ') && (var6 <= '<' || var6 >= ']') && (var6 <= '&' || var6 >= '<') && var6 != '\t' && (var6 < '\ue000' || var6 > 'ÔøΩ')) {
  409.                if (var6 == '<' || var6 == '&') {
  410.                   break;
  411.                }
  412.  
  413.                if (var6 == '\n') {
  414.                   if (!this.isInternal()) {
  415.                      ++this.lineNumber;
  416.                   }
  417.                } else if (var6 == '\r') {
  418.                   if (!this.isInternal()) {
  419.                      var1.characters(this.buf, var3, var4 - var3);
  420.                      var1.characters(newline, 0, 1);
  421.                      var5 = true;
  422.                      ++this.lineNumber;
  423.                      if (this.finish > var4 + 1 && this.buf[var4 + 1] == '\n') {
  424.                         ++var4;
  425.                      }
  426.  
  427.                      var3 = this.start = var4 + 1;
  428.                   }
  429.                } else if (var6 == ']') {
  430.                   switch (this.finish - var4) {
  431.                      case 2:
  432.                         if (this.buf[var4 + 1] != ']') {
  433.                            break;
  434.                         }
  435.                      case 1:
  436.                         if (this.reader != null && !this.isClosed) {
  437.                            if (var4 == var3) {
  438.                               throw new InternalError("fillbuf");
  439.                            }
  440.  
  441.                            --var4;
  442.                            if (var4 > var3) {
  443.                               var2.text();
  444.                               var1.characters(this.buf, var3, var4 - var3);
  445.                               var5 = true;
  446.                               this.start = var4;
  447.                            }
  448.  
  449.                            this.fillbuf();
  450.                            var3 = var4 = this.start;
  451.                         }
  452.                         break;
  453.                      default:
  454.                         if (this.buf[var4 + 1] == ']' && this.buf[var4 + 2] == '>') {
  455.                            this.fatal("P-072", (Object[])null);
  456.                         }
  457.                   }
  458.                } else if (var6 >= '\ud800' && var6 <= '\udfff') {
  459.                   if (var4 + 1 >= this.finish) {
  460.                      if (var4 > var3) {
  461.                         var2.text();
  462.                         var1.characters(this.buf, var3, var4 - var3);
  463.                         var5 = true;
  464.                         this.start = var4 + 1;
  465.                      }
  466.  
  467.                      if (this.isEOF()) {
  468.                         this.fatal("P-081", new Object[]{Integer.toHexString(var6)});
  469.                      }
  470.  
  471.                      var3 = this.start;
  472.                      var4 = var3;
  473.                   } else {
  474.                      if (!this.checkSurrogatePair(var4)) {
  475.                         --var4;
  476.                         break;
  477.                      }
  478.  
  479.                      ++var4;
  480.                   }
  481.                } else {
  482.                   this.fatal("P-071", new Object[]{Integer.toHexString(var6)});
  483.                }
  484.             }
  485.          }
  486.  
  487.          ++var4;
  488.       }
  489.  
  490.       if (var4 == var3) {
  491.          return var5;
  492.       } else {
  493.          var2.text();
  494.          var1.characters(this.buf, var3, var4 - var3);
  495.          this.start = var4;
  496.          return true;
  497.       }
  498.    }
  499.  
  500.    public boolean peek(String var1, char[] var2) throws IOException, SAXException {
  501.       int var3;
  502.       if (var2 != null) {
  503.          var3 = var2.length;
  504.       } else {
  505.          var3 = var1.length();
  506.       }
  507.  
  508.       if (this.finish <= this.start || this.finish - this.start < var3) {
  509.          this.fillbuf();
  510.       }
  511.  
  512.       if (this.finish <= this.start) {
  513.          return false;
  514.       } else {
  515.          int var4;
  516.          if (var2 != null) {
  517.             for(var4 = 0; var4 < var3 && this.start + var4 < this.finish; ++var4) {
  518.                if (this.buf[this.start + var4] != var2[var4]) {
  519.                   return false;
  520.                }
  521.             }
  522.          } else {
  523.             for(var4 = 0; var4 < var3 && this.start + var4 < this.finish; ++var4) {
  524.                if (this.buf[this.start + var4] != var1.charAt(var4)) {
  525.                   return false;
  526.                }
  527.             }
  528.          }
  529.  
  530.          if (var4 < var3) {
  531.             if (this.reader != null && !this.isClosed) {
  532.                if (var3 > this.buf.length) {
  533.                   this.fatal("P-077", new Object[]{new Integer(this.buf.length)});
  534.                }
  535.  
  536.                this.fillbuf();
  537.                return this.peek(var1, var2);
  538.             } else {
  539.                return false;
  540.             }
  541.          } else {
  542.             this.start += var3;
  543.             return true;
  544.          }
  545.       }
  546.    }
  547.  
  548.    public boolean peekc(char var1) throws IOException, SAXException {
  549.       if (this.finish <= this.start) {
  550.          this.fillbuf();
  551.       }
  552.  
  553.       if (this.finish > this.start) {
  554.          if (this.buf[this.start] == var1) {
  555.             ++this.start;
  556.             return true;
  557.          } else {
  558.             return false;
  559.          }
  560.       } else {
  561.          return false;
  562.       }
  563.    }
  564.  
  565.    public InputEntity pop() throws IOException {
  566.       this.close();
  567.       return this.next;
  568.    }
  569.  
  570.    public String rememberText() {
  571.       String var1;
  572.       if (this.rememberedText != null) {
  573.          this.rememberedText.append(this.buf, this.startRemember, this.start - this.startRemember);
  574.          var1 = this.rememberedText.toString();
  575.       } else {
  576.          var1 = new String(this.buf, this.startRemember, this.start - this.startRemember);
  577.       }
  578.  
  579.       this.startRemember = 0;
  580.       this.rememberedText = null;
  581.       return var1;
  582.    }
  583.  
  584.    public void startRemembering() {
  585.       if (this.startRemember != 0) {
  586.          throw new InternalError();
  587.       } else {
  588.          this.startRemember = this.start;
  589.       }
  590.    }
  591.  
  592.    public void ungetc() {
  593.       if (this.start == 0) {
  594.          throw new InternalError("ungetc");
  595.       } else {
  596.          --this.start;
  597.          if (this.buf[this.start] != '\n' && this.buf[this.start] != '\r') {
  598.             if (this.returnedFirstHalf) {
  599.                this.returnedFirstHalf = false;
  600.             }
  601.          } else if (!this.isInternal()) {
  602.             --this.lineNumber;
  603.          }
  604.  
  605.       }
  606.    }
  607.  
  608.    public boolean unparsedContent(DocumentHandler var1, ElementValidator var2, boolean var3, String var4) throws IOException, SAXException {
  609.       if (!this.peek("![CDATA[", (char[])null)) {
  610.          return false;
  611.       } else {
  612.          if (var1 instanceof LexicalEventListener) {
  613.             ((LexicalEventListener)var1).startCDATA();
  614.          }
  615.  
  616.          while(true) {
  617.             boolean var6 = false;
  618.             boolean var8 = var3;
  619.  
  620.             int var5;
  621.             for(var5 = this.start; var5 < this.finish; ++var5) {
  622.                char var7 = this.buf[var5];
  623.                if (!XmlChars.isChar(var7)) {
  624.                   var8 = false;
  625.                   if (var7 >= '\ud800' && var7 <= '\udfff') {
  626.                      if (!this.checkSurrogatePair(var5)) {
  627.                         --var5;
  628.                         break;
  629.                      }
  630.  
  631.                      ++var5;
  632.                      continue;
  633.                   }
  634.  
  635.                   this.fatal("P-071", new Object[]{Integer.toHexString(this.buf[var5])});
  636.                }
  637.  
  638.                if (var7 == '\n') {
  639.                   if (!this.isInternal()) {
  640.                      ++this.lineNumber;
  641.                   }
  642.                } else if (var7 == '\r') {
  643.                   if (!this.isInternal()) {
  644.                      if (var8) {
  645.                         if (var4 != null) {
  646.                            this.errHandler.error(new SAXParseException(Parser.messages.getMessage(this.locale, var4), this));
  647.                         }
  648.  
  649.                         var1.ignorableWhitespace(this.buf, this.start, var5 - this.start);
  650.                         var1.ignorableWhitespace(newline, 0, 1);
  651.                      } else {
  652.                         var2.text();
  653.                         var1.characters(this.buf, this.start, var5 - this.start);
  654.                         var1.characters(newline, 0, 1);
  655.                      }
  656.  
  657.                      ++this.lineNumber;
  658.                      if (this.finish > var5 + 1 && this.buf[var5 + 1] == '\n') {
  659.                         ++var5;
  660.                      }
  661.  
  662.                      this.start = var5 + 1;
  663.                   }
  664.                } else if (var7 != ']') {
  665.                   if (var7 != ' ' && var7 != '\t') {
  666.                      var8 = false;
  667.                   }
  668.                } else if (var5 + 2 < this.finish) {
  669.                   if (this.buf[var5 + 1] == ']' && this.buf[var5 + 2] == '>') {
  670.                      var6 = true;
  671.                      break;
  672.                   }
  673.  
  674.                   var8 = false;
  675.                } else {
  676.                   --var5;
  677.                   this.fillbuf();
  678.                   var5 = this.start - 1;
  679.                }
  680.             }
  681.  
  682.             if (var8) {
  683.                if (var4 != null) {
  684.                   this.errHandler.error(new SAXParseException(Parser.messages.getMessage(this.locale, var4), this));
  685.                }
  686.  
  687.                var1.ignorableWhitespace(this.buf, this.start, var5 - this.start);
  688.             } else {
  689.                var2.text();
  690.                var1.characters(this.buf, this.start, var5 - this.start);
  691.             }
  692.  
  693.             if (var6) {
  694.                this.start = var5 + 3;
  695.                if (var1 instanceof LexicalEventListener) {
  696.                   ((LexicalEventListener)var1).endCDATA();
  697.                }
  698.  
  699.                return true;
  700.             }
  701.  
  702.             this.start = var5;
  703.             if (this.isEOF()) {
  704.                this.fatal("P-073", (Object[])null);
  705.             }
  706.          }
  707.       }
  708.    }
  709. }
  710.